<template>
  <div>
    <!-- 面包屑导航 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/index/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>菜单管理</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 搜索筛选 -->
    <el-form :inline="true" :model="formInline" class="menu-search">
      <el-form-item label="">
        <el-input size="small" v-model="formInline.menuName" placeholder="输入菜单名"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button size="small" type="primary" icon="el-icon-search" @click="search">搜索</el-button>
        <el-button size="small" type="primary" icon="el-icon-plus" @click="handleEdit()">添加</el-button>
      </el-form-item>
    </el-form>
    <!--列表-->
    <el-table size="small" @selection-change="selectChange" :data="menuData" highlight-current-row v-loading="loading"
      element-loading-text="拼命加载中" style="width: 100%">
      <el-table-column type="selection" width="50"> </el-table-column>
      <el-table-column sortable prop="menuId" label="菜单号" width="200">
      </el-table-column>
      <el-table-column sortable prop="menuName" label="菜单名" width="200">
      </el-table-column>
      <el-table-column sortable prop="menuType" label="菜单类型" width="200">
      </el-table-column>
      
      <el-table-column label="操作" min-width="100">
        <template slot-scope="scope">
          <el-button size="mini" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
          <el-button size="mini" type="danger" @click="deleteMenu(scope.$index, scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页组件 -->
    <Pagination v-bind:child-msg="pageparm" @callFather="callFather"></Pagination>
    <!-- 编辑界面 -->
    <el-dialog :title="title" :visible.sync="editFormVisible" width="30%" @click="closeDialog('edit')">
      <el-form label-width="80px" ref="editForm" :model="editForm" :rules="rules">
        <el-form-item label="菜单号" prop="menuId">
          <el-input size="small" v-model="editForm.menuId" placeholder="请输入菜单号"></el-input>
        </el-form-item>
        <el-form-item label="菜单名" prop="menuName">
          <el-input size="small" v-model="editForm.menuName" auto-complete="off" placeholder="请输入菜单名"></el-input>
        </el-form-item>
        
        <!-- <el-form-item label="角色" prop="roleId">
          <el-select size="small" v-model="editForm.roleId" placeholder="请选择" class="menuRole">
            <el-option label="超级管理员" value="1"></el-option>
            <el-option label="管理员" value="2"></el-option>
          </el-select>
        </el-form-item> -->
        <el-form-item label="菜单类型" prop="menuType">
          <el-input size="small" v-model="editForm.menuType" placeholder="请输入菜单类型"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button size="small" @click="closeDialog('edit')">取消</el-button>
        <el-button size="small" type="primary" :loading="loading" class="title" @click="submitForm('editForm')">保存
        </el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  // 导入请求方法
  import {
    menuList,
    menuInsert,
    getTree,
    menuTree,
    menuUpdate,
    menuDelete
  } from "@/api/menu";
  // import {
  //   roleList
  // } from '@/api/role'
  import Pagination from "@/components/Pagination";
  export default {
    data() {
      return {
        loading: false, //是否显示加载
        title: "添加菜单",
        editFormVisible: false, //控制编辑页面显示与隐藏
        // 编辑与添加form表单
        editForm: {
          menuId: 0,
          menuName: null,
          menuUrl: null,
          menuType: 0,
          menuLogo: null,
        },
        editForm_1: {
          menuId: 0,
          menuName: null,
          menuUrl: null,
          menuType: 0,
          menuLogo: null,
        },
        // 角色对象
        role: {
          roleId: "",
          roleName: "",
        },
        // rules表单验证
        rules: {
          menuName: [{
            required: true,
            message: "请输入菜单名",
            trigger: "blur",
          }, ],
          roleId: [{
            required: true,
            message: "请选择角色",
            trigger: "blur",
          }, ],
          
        },
        // 搜索用参数
        formInline: {
          pageNum: 1,
          pageSize: 20,
          menuName: "",
        },
        //用户列表数据
        menuData: [],
        defaultProps: {
          children: "children",
          label: "name",
        },
        // 选中
        checkmenu: [],
        // 分页参数
        pageparm: {
          currentPage: 1,
          pageSize: 5,
          total: 3,
        },
      };
    },
    // 注册组件
    components: {
      Pagination,
    },
    /**
     * 数据发生改变
     */
    watch: {},

    /**
     * 使用api获得menuList
     */
    mounted() {
      this.getdata(this.formInline);
    },

    /**
     * 里面的方法只有被调用才会执行
     */
    methods: {
      // 获取数据方法
      getdata(parameter) {
        this.loading = true;
        // 获取菜单列表
        menuList(parameter).then((res) => {
          this.loading = false;
          if (res.code != 200) {
            this.$message({
              type: "error",
              message: res.message,
            });
          } else {
            this.menuData = res.data.list;
            // 分页赋值
            this.pageparm.currentPage = this.formInline.pageNum;
            this.pageparm.pageSize = this.formInline.pageSize;
            this.pageparm.total = res.data.total;
          }
        });
      },
      // 分页插件事件
      callFather(parm) {
        this.formInline.page = parm.currentPage;
        this.formInline.limit = parm.pageSize;
        this.getdata(this.formInline);
      },
      //搜索事件->[暂时不做]
      search() {
        this.getdata(this.formInline);
        
      },
      // 选择复选框事件
      selectChange(val) {
        this.selectdata = val;
      },
      //显示编辑界面
      handleEdit: function (index, row) {
        this.editFormVisible = true;
        if (row != undefined && row != "undefined") {
          this.title = "修改菜单";
          this.editForm.menuId = row.menuId;
          this.editForm.menuName = row.menuName;
          this.editForm.menuUrl = row.menuUrl;
          this.editForm.menuType = row.menuType;
        } else {
          this.title = "添加菜单";
          this.editForm.menuId = '';
          this.editForm.menuName = '';
          this.editForm.menuUrl= '';
          this.editForm.menuType = '';
        }
      },
      // 编辑、添加提交方法
      submitForm(editData) {
      this.$refs[editData].validate((valid) => {
        if (valid) {
          console.log(this.editForm_1);
          this.editForm_1.menuName = this.editForm.menuName;
          this.editForm_1.menuId = this.editForm.menuId;
          this.editForm_1.menuLogo = this.editForm.menuLogo;
          this.editForm_1.menuType = this.editForm.menuType;
          this.editForm_1.menuUrl = this.editForm.menuUrl;
          if (this.editForm.menuId != 0) {
            //insert
            menuInsert(this.editForm_1).then((res) => {
              if (res.code != 200) {
                this.$message({
                  type: "error",
                  message: res.message,
                });
              } else {
                this.$message({
                  type: "success",
                  message: "菜单添加成功！",
                })
                this.getdata(this.formInline); //重新获取菜单列表
                this.editFormVisible = false; //隐藏表单
              }
            });
          } else {
            //update
            menuUpdate(this.editForm).then((res) => {
              if (res.code != 200) {
                this.$message({
                  type: "error",
                  message: res.message,
                });
              } else {
                this.$message({
                  type: "success",
                  message: "菜单修改成功！",
                });
                this.getdata(this.formInline); //重新获取⽤⼾列表
                this.editFormVisible = false; //隐藏表单
              }
            });
          }
        } else {
          //验证错误！
          this.$message({
            type: "info",
            message: "验证错误！",
          });
        }
      });
    },

      // 关闭编辑、增加弹出框
      closeDialog(dialog) {
        if (dialog == "edit") {
          this.editFormVisible = false;
        }
      },
      // 删除菜单
      deleteMenu(index, row) {
        this.$confirm('确定要删除吗?', '信息', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          //调用menuDelete删除菜单信息
          let menuId = row.menuId;
          menuDelete({
            menuId
          }).then(res => {
            if (res.code != 200) {
              this.$message({
                type: 'error',
                message: res.message
              })
            } else {
              this.$message({
                type: 'success',
                message: '菜单删除成功！'
              })
              this.getData(this.formInline) //重新获取⽤⼾列表
            }
          })
        })
      },
    }
  };
</script>

<style scoped>
  .menu-search {
    margin-top: 20px;
  }

  .menuRole {
    width: 100%;
  }
</style>